package org.jeecg.modules.cosmetic.handler;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.cosmetic.entity.TToxicologicalReport;
import org.jeecg.modules.cosmetic.model.dto.ToxReportDTO;
import org.jeecg.modules.cosmetic.model.vo.ToxReportVO;
import org.jeecg.modules.cosmetic.service.ITToxicologicalReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ToxicologicalReportHandler {

    @Resource
    private ITToxicologicalReportService toxicologicalReportService;

    /**
     * 删除报告
     * @param id
     * @return
     */
    public boolean delete(Long id) {
        return toxicologicalReportService.removeById(id);
    }

    /**
     * 查询报告信息
     * @param params
     * @return
     */
    public List<ToxReportDTO> getToxReport(String params) {
        List<TToxicologicalReport> list = toxicologicalReportService.list(new LambdaQueryWrapper<TToxicologicalReport>().eq(TToxicologicalReport::getForm, 0)
                .and(StringUtils.isNotBlank(params), w -> w.like(TToxicologicalReport::getName, params)
                        .or().like(TToxicologicalReport::getYears, params))
                .orderByDesc(TToxicologicalReport::getId).last("limit 10"));
        List<ToxReportDTO> reportList = Lists.newArrayList();
        return list.stream().map(m -> {
            ToxReportDTO dto = new ToxReportDTO();
            BeanUtils.copyProperties(m, dto);
            return dto;
        }).collect(Collectors.toCollection(() -> reportList));

        // 过滤掉同名的数据
        // return reportList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ToxReportDTO::getName))), ArrayList::new));
    }

    /**
     * 保存报告
     * @param vo
     * @return
     */
    public boolean save(ToxReportVO vo) {
        TToxicologicalReport toxicologicalReport = new TToxicologicalReport();
        BeanUtils.copyProperties(vo, toxicologicalReport);
        toxicologicalReport.setForm(0);
        return toxicologicalReportService.save(toxicologicalReport);
    }

}
